var content = document.querySelector('.content');
var tbody = document.querySelector(".grade-table tbody");
var searchBtn = document.getElementById('searchBtn');

content.addEventListener('mouseenter', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.5)'; // 将透明度更改为 0.5，使背景色加深
});

content.addEventListener('mouseleave', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.2)'; // 将透明度恢复为 0.2，使背景色变浅
});

var data = [];
//data = [
//  { 课程序号: "01", 课程名称: "数据结构", 课程专业: "软件工程", 上课时间: "周一8:00-10:00", 授课教师: "孔老师" },
//  { 课程序号: "12", 课程名称: "程序设计实践", 课程专业: "软件工程", 上课时间: "周三16:00-18:00", 授课教师: "崔老师" },
//  { 课程序号: "25", 课程名称: "化妆品安全与健康", 课程专业: "海洋科学与技术", 上课时间: "周五19:00-21:00", 授课教师: "朱老师" }
//];
document.addEventListener("DOMContentLoaded", function() {

    $.ajax({
      url: '/management/stuservlet/CourseChoose',
      method: 'POST',
      data: {
        username: "1"
      },
      success: function(response) {
        console.log(response);
        console.log(response.data);
        data = response.data;
        // 将数据数组赋值给相应的元素
        var currentIndex = 1;
        data.forEach(function(item) {
          var row = document.createElement("tr");
          row.innerHTML = `
            <td>${currentIndex}</td>
            <td>${item.Cou_id}</td>
            <td>${item.Cou_name}</td>
            <td>${item.Cou_major}</td>
            <td>${item.Cou_time}</td>
            <td>${item.Tea_name}</td>
            <td><button id="jjw${currentIndex}">选课</button></td>
          `;
          tbody.appendChild(row);
          currentIndex++;
         // console.log(jjw${currentIndex});
        });
        for (let i=1; i < currentIndex; i++) {
              var jjw = document.getElementById('jjw'+i);
            //  console.log(jjw);
              jjw.addEventListener('click', function() {
                // 编写跳转到修改信息页面的操作
                // window.location.href = "editstupwd.html";
                  var confirmLogout = confirm("确定要选课吗？");
                    if (confirmLogout) {
                      // 用户点击了确认按钮
                      // 编写退出登录的操作
                      //没选过这个课
                      $.ajax({
                            url: '/management/stuservlet/GetCourse',
                            method: 'POST',
                            data: {
                              count: i
                            },
                            success: function(response) {
                              console.log(response);
                              console.log(response.data);
//                              alert("选课成功！"+i);
                              alert("选课成功！");
                            }
                          });
                    } else {
                      // 用户点击了取消按钮
                      // 可以选择执行其他操作或不进行任何操作
                    }
              });
            }
        }
    });

  var links = document.querySelectorAll('.sidebar ul li a');

  for (var i = 0; i < links.length; i++) {
    links[i].addEventListener('mouseenter', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '#679';
      this.style.borderRadius = '10px'; // 设置边缘的圆角半径为10px
    });

    links[i].addEventListener('mouseleave', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '';
      this.style.borderRadius = '10px'; // 鼠标移出时恢复原始边缘
    });
  }
});

$(document).keydown(function(event) {
  if (event.keyCode == 13) { // 13代表回车键的键码
    $('#searchBtn').click();
  }
});

searchBtn.addEventListener('click', function() {
    var searchInput = document.getElementById("search");
    var searchText = searchInput.value;
    $.ajax({
        url: '/management/stuservlet/searchByCouNameEX',
        method: 'POST',
        data: {
            searchText: searchText
        },
        success: function(response) {
            console.log(response);
            var search = response.data;
            console.log(search);
            if(search.length == 0){
                alert("没有符合要求的课程！");
            }
            else{
            tbody.innerHTML = "";
            currentIndex = 1;
            search.forEach(function(item) {
                var row = document.createElement("tr");
                row.innerHTML = `
                    <td>${currentIndex}</td>
                    <td>${item.Cou_id}</td>
                    <td>${item.Cou_name}</td>
                    <td>${item.Cou_major}</td>
                    <td>${item.Cou_time}</td>
                    <td>${item.Tea_name}</td>
                    <td><button id="jjw${currentIndex}">选课</button></td>
                `;
                tbody.appendChild(row);
                currentIndex++;
            });
            for (let i = 1; i < currentIndex; i++) {
                var jjw = document.getElementById('jjw' + i);
                jjw.addEventListener('click', function() {
                    var confirmLogout = confirm("确定要选课吗？");
                    if (confirmLogout) {
                        $.ajax({
                            url: '/management/stuservlet/GetCourse',
                            method: 'POST',
                            data: {
                                count: i
                            },
                            success: function(response) {
                                console.log(response);
                                console.log(response.data);
                                alert("选课成功！");
                            }
                        });
                    } else {
                        // 用户点击了取消按钮
                        // 可以选择执行其他操作或不进行任何操作
                    }
                });
            }
            }
        }
    });
});
